package com.itcoon.cloud.framework.springdoc.plugin;/**
 * @author Athric Yu
 * 2024/11/12
 **/

import io.swagger.v3.core.util.PrimitiveType;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import org.springframework.beans.BeanUtils;

/**
 *
 *
 * @author Athric Yu
 * 2024/11/12
 **/
public interface TemporalCustomizer {

    default <V> Schema<V> getSchema(Class<V> primitiveType, Schema<?> sourceSchema){
        Schema<Object> schema;
        PrimitiveType item = PrimitiveType.fromType(primitiveType);

        if (item == null) {
            schema = new ObjectSchema();
        } else {
            schema = item.createProperty();
        }

        // 获取schema的type和format
        String schemaType = schema.getType();
        String format = schema.getFormat();
        // 复制原schema的其它属性
        BeanUtils.copyProperties(sourceSchema, schema);

        // 使用根据枚举值类型获取到的schema
        return schema.type(schemaType).format(format);
    }
}
